<?php

namespace App\View\Components;

use Illuminate\Support\Facades\Request;
use Illuminate\View\Component;

class Code extends Component
{
    public $value;
    public $showAll;
    public $view;
    public $kv;
    public $name;

    /**
     * Create a new component instance.
     *
     * @return void
     */
    public function __construct($code, $name, $noIds = '', $value = '', $showAll = '1', $hasKeys = '', $view = 'select')
    {

        $this->view = $view;
        $this->name = $name;
        if (empty($noIds)) {
            $noIds = [];
        } else {
            $noIds = explode(',', $noIds);
        }
        if (empty($hasKeys)) {
            $hasKeys = true;
        } else {
            $hasKeys = explode(',', $hasKeys);
        }
        $this->showAll = $showAll;
        if (empty($value)) {
            $value = Request::input($name, '');
        }
        $this->value = $value;
        $this->kv = \App\Facade\Code::kv($code, $noIds, $hasKeys);
    }

    /**
     * Get the view / contents that represent the component.
     *
     * @return \Illuminate\Contracts\View\View|\Closure|string
     */
    public function render()
    {
        return view('components.code_' . $this->view);
    }
}
